package com.example.demo;


import lombok.Data;


import java.text.SimpleDateFormat;


/**
 *
 * 部件状态
 *
 * @author youngki
 * @date 2022/10/09
 */
@Data
public class DeviceStatusGuoBiao{


	private Integer status;

	private UserMessage userMessage;
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public DeviceStatusGuoBiao(String message, String deviceId) {


		userMessage = new UserMessage();
		userMessage.setSystemType(Integer.parseInt(message.substring(0, 2), 16));
		userMessage.setFireMainCode(Integer.parseInt(message.substring(2, 4), 16));
		userMessage.setPartType(Integer.parseInt(message.substring(4, 6), 16));
		userMessage.setAddressNo(String.valueOf(Integer.parseInt(message.substring(8, 10) +
				message.substring(6, 8), 16)));

		userMessage.setLoopNo(String.valueOf(Integer.parseInt(message.substring(12, 14) +
				message.substring(10, 12), 16)));
		String bit_stat = ByteUtil.buqi(Integer.toBinaryString(Integer.parseInt(message.substring(14, 18), 16)));
		status = 2;
		if (bit_stat.substring(0, 1).equals("1")) {
			userMessage.setPartStat("延时");
		} else if (bit_stat.substring(1, 2).equals("1")) {
			userMessage.setPartStat("反馈");
			status = 1;
		} else if (bit_stat.substring(2, 3).equals("1")) {
			userMessage.setPartStat("启动");
			status = 1;
		}  else if (bit_stat.substring(3, 4).equals("1")) {
			userMessage.setPartStat("监管");
			status = 1;
		} else if (bit_stat.substring(4, 5).equals("1")) {
			userMessage.setPartStat("屏蔽");
		} else if (bit_stat.substring(5, 6).equals("1")) {
			userMessage.setPartStat("故障");
			status = 0;
		} else if (bit_stat.substring(6, 7).equals("1")) {
			userMessage.setPartStat("火警");
			status = 1;
		} else if (bit_stat.substring(7, 8).equals("1")) {
			userMessage.setPartStat("测试运行");
		} else if (bit_stat.substring(14, 15).equals("1")) {
			userMessage.setPartStat("手动");
		}  else if (bit_stat.substring(15, 16).equals("1")) {
			userMessage.setPartStat("电源故障");
			status = 0;
		} else {
			userMessage.setPartStat("正常");
		}
		//设备台账信息没有，解析出来都是空格
		String addressDesc = ByteUtil.gB18030ByteArrayToUtf8String(ByteUtil.hexStringToByteArray(message.substring(18, 80))).replace("\u0000", "").replace("\uE5E5", "");
		userMessage.setPartExplain(addressDesc);


		String time = message.substring(80);
		String ms = String.valueOf(Integer.parseInt(time.substring(0, 2), 16));
		String min = String.valueOf(Integer.parseInt(time.substring(2, 4), 16));
		String hour = String.valueOf(Integer.parseInt(time.substring(4, 6), 16));
		String day = String.valueOf(Integer.parseInt(time.substring(6, 8), 16));
		String month = String.valueOf(Integer.parseInt(time.substring(8, 10), 16));
		String year = String.valueOf(Integer.parseInt(time.substring(10), 16));

		userMessage.setUpTime("20" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + ms);



	}
}
